package com.jifeng.sterilizer.service.impl;

import com.jifeng.sterilizer.utils.HttpUtils;
import com.jifeng.sterilizer.utils.R;
import com.jifeng.sterilizer.vo.FarmListVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jifeng.sterilizer.utils.PageUtils;
import com.jifeng.sterilizer.utils.Query;

import com.jifeng.sterilizer.dao.FarmDao;
import com.jifeng.sterilizer.entity.FarmEntity;
import com.jifeng.sterilizer.service.FarmService;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;


@Service("farmService")
@Slf4j
public class FarmServiceImpl extends ServiceImpl<FarmDao, FarmEntity> implements FarmService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<FarmEntity> page = this.page(
                new Query<FarmEntity>().getPage(params),
                new QueryWrapper<FarmEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public R getFarmList(Map<String, Object> params) {
        try {
            String province = (String) params.get("province");
            String city = (String) params.get("city");
//            String district = (String) params.get("district");
            if (StringUtils.isEmpty(province) || StringUtils.isEmpty(city)) {
                return R.error(400, "省或市不得为空！");
            }
            List<FarmEntity> farmEntityList = this.list(new QueryWrapper<FarmEntity>().eq("province", province).eq("city", city));
            List<FarmListVo> farmListVoList = farmEntityList.stream().map(farmEntity -> {
                FarmListVo farmListVo = new FarmListVo(farmEntity.getId(), farmEntity.getName(), farmEntity.getProvince(), farmEntity.getCity(), farmEntity.getDistrict());
                return farmListVo;
            }).collect(Collectors.toList());
            return R.ok().put("data", farmListVoList);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("方法：{}，错误信息：{}", "获取农场列表", e.getMessage());
            return R.error(400, "系统繁忙");
        }
    }

    @Override
    public R getFarmListByName(Map<String, Object> params) {
        Object name = params.get("name");
        //  如果当前登录用户类型type==3，则只能返回关联农场名，在根据用户名查询农场表中的农场名
        HttpServletRequest request = getRequest();
        // 获得token中的用户信息
        Map tokenInfo = HttpUtils.getTokenInfo(request);
        Object type = tokenInfo.get("type");
        Object farmId = tokenInfo.get("farmId");
        QueryWrapper<FarmEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name", name);
        if (type.toString().equals("3")) {
            queryWrapper.eq("id", farmId);
        }
        List<FarmEntity> farmEntityList = this.list(queryWrapper);
        if (farmEntityList.size() > 10) {
            farmEntityList = farmEntityList.subList(0, 10);
        }
        List<Map<String, Object>> mapList = farmEntityList.stream().map(farmEntity -> {
            Map<String, Object> map = new HashMap<>();
            map.put("id", farmEntity.getId());
            map.put("name", farmEntity.getName());
            return map;
        }).collect(Collectors.toList());
        return R.ok().put("data", mapList);
    }

    private HttpServletRequest getRequest() {
        return ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
    }
}